<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class HuatiOwner extends Model
{

    public $timestamps = false;
    protected $table = 'tbl_huati_owner';

    ######关联关系 start#######
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'c_id');
    }

    public function huati()
    {
        return $this->belongsTo(Huati::class, 'huati_id', 'id');
    }

    ######关联关系  end########


    const GENERAL = 1;
    const SILVER = 2;
    const GOLD = 3;

    static $LevelDisplay = [
        self::GENERAL => '侣长',
        self::SILVER => '银牌侣长',
        self::GOLD => '金牌侣长',
    ];

    /**
     * 未分配的话题列表
     * @return array
     */
    static function HuatiList()
    {
        //已经分配圈主
        $huati_owns = HuatiOwner::pluck('huati_id')->toArray();
        //未分配的话题
        return Huati::select('id', 'title')->whereNotIn('id', $huati_owns)->get()->toArray();
    }


    /**
     * 设置圈主
     * @param $user_id
     * @param $huati_id
     * @return array
     */
    static function HuatiOwnerAdd($user_id, $huati_id)
    {
       $title = Huati::where('id', $huati_id)->value('title');
       $huati_owner = new HuatiOwner();
       $huati_owner->user_id = $user_id;
       $huati_owner->huati_id = $huati_id;
       $huati_owner->circle_name = $title;
       $huati_owner->level = self::GENERAL;
       if (!$huati_owner->save()) {
           return ['status' => 2, 'message' => '失败'];
       }
        return ['status' => 1, 'message' => '成功'];
    }
}
